package com.hxy.manager.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.hxy.manager.R;
import com.hxy.manager.activity.bill.Bill;
import com.hxy.manager.activity.goods.GoodsAddActivity;
import com.hxy.manager.activity.goods.GoodsShowActivity;
import com.hxy.manager.activity.goods.GoodsStoreActivity;
import com.hxy.manager.activity.today.TodayActivity;
import com.hxy.manager.entity.GoodsEntity;
import com.hxy.manager.menu.ResideMenu;
import com.hxy.manager.menu.ResideMenuItem;
import com.hxy.manager.service.GoodsEntityService;


/**
 * Created by 傲寒宏志 on 2017/12/30.
 */

public class IndexActivity extends Activity {
    ResideMenu resideMenu;
    private ResideMenuItem itemHome;
    private ResideMenuItem itemProfile;
    private ResideMenuItem itemCalendar;
    private ResideMenuItem itemSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//这行代码必须在setContentView之前
        setContentView(R.layout.index);

        menu();

        //        检查权限
        if (ContextCompat.checkSelfPermission(IndexActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(IndexActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
        }

        if (ContextCompat.checkSelfPermission(IndexActivity.this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(IndexActivity.this, new String[]{Manifest.permission.INTERNET}, 1);
        }else {
            Log.i("kkk", "网络权限已经有了");

        }

        
        /*
       	//读取shareperf
      	SharedPreferences pref= getSharedPreferences("com.hxy.manager_preferences",MODE_PRIVATE);
      	String string =pref.getString("ServerUrl", null);
        Log.i("kkk", "pref1"+pref+string);

        */
        
        
        
        /*
           //setTitle("薛家岗新店管理中心"); 
          	//去掉状态栏
         
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN  
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar  
                | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar  
                // remove the following flag for version < API 19  
                | View.SYSTEM_UI_FLAG_IMMERSIVE);  
     */
    }

    //侧滑菜单
    public void menu() {
        // attach to current activity;
        resideMenu = new ResideMenu(this);
        // resideMenu.setBackground(R.drawable.menu_background);
        //主页的背景
        //resideMenu.setBackground(R.drawable.wallpaper);
        resideMenu.setBackgroundColor(0xFFFFFF);
        resideMenu.attachToActivity(this);
        // create menu items;
        itemHome = new ResideMenuItem(this, R.drawable.icon_home, "Home");
        itemProfile = new ResideMenuItem(this, R.drawable.icon_profile, "用户名");
        itemCalendar = new ResideMenuItem(this, R.drawable.icon_calendar, "Calendar");
        itemSettings = new ResideMenuItem(this, R.drawable.icon_settings, "设置");
        itemHome.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "主页", Toast.LENGTH_SHORT).show();
                resideMenu.closeMenu();//关闭菜单
            }
        });
        itemProfile.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "简历", Toast.LENGTH_SHORT).show();
            }
        });
        itemCalendar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "日历", Toast.LENGTH_SHORT).show();

            }
        });
        itemSettings.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(IndexActivity.this, SettingActivity.class));
                //Toast.makeText(getApplicationContext(), "设置", Toast.LENGTH_SHORT).show();
                resideMenu.closeMenu();//关闭菜单
            }
        });


        resideMenu.addMenuItem(itemHome, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemProfile, ResideMenu.DIRECTION_LEFT);
        resideMenu.addMenuItem(itemCalendar, ResideMenu.DIRECTION_RIGHT);
        resideMenu.addMenuItem(itemSettings, ResideMenu.DIRECTION_RIGHT);


        // You can disable a direction by setting ->
        // resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);

        findViewById(R.id.title_bar_left_menu).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
            }
        });
        findViewById(R.id.title_bar_right_menu).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                resideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);
            }
        });
        
        /*
        // create menu items;
        String titles[] = { "Home", "Profile", "Calendar", "Settings" };
        int icon[] = { R.drawable.icon_home, R.drawable.icon_profile, R.drawable.icon_calendar, R.drawable.icon_settings };

        for (int i = 0; i < titles.length; i++){
            ResideMenuItem item[i] = new ResideMenuItem(this, icon[i], titles[i]);
            item.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					if(v==itemHome){
						
					}
					// TODO Auto-generated method stub
					Toast.makeText(getApplicationContext(), "点击成功", Toast.LENGTH_SHORT).show();
				}
            	
            });
            */
        //    resideMenu.addMenuItem(item,  ResideMenu.DIRECTION_LEFT); // or  ResideMenu.DIRECTION_RIGHT
        // }


    }

    //返回键的监听
    @Override
    public void onBackPressed() {
        if (resideMenu.isOpened()) {
            resideMenu.closeMenu();
        } else
            finish();
    }

    // What good method is to access resideMenu？
    public ResideMenu getResideMenu() {
        return resideMenu;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return resideMenu.dispatchTouchEvent(ev);
    }

    private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
        @Override
        public void openMenu() {
            Toast.makeText(getApplicationContext(), "Menu is opened!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void closeMenu() {
            Toast.makeText(getApplicationContext(), "Menu is closed!", Toast.LENGTH_SHORT).show();
        }
    };


    //存档
    public void save(View v) {
        Intent intent = new Intent(IndexActivity.this, GoodsAddActivity.class);
        startActivity(intent);
        //   Toast.makeText(this,"点击了存档",Toast.LENGTH_LONG).show();

    }

    //登记
    public void record(View v) {
        Intent intent = new Intent(IndexActivity.this, RecordActivity.class);
        startActivity(intent);
    }

    //账单
    public void bill(View v) {
        Intent intent = new Intent(IndexActivity.this, Bill.class);
        startActivity(intent);
    }

    //扫一扫
    public void saoyisao(View v) {

        //  安卓6.0以上需要运行时候申请权限，亟待解决！ ·
        //  if (ContextCompat.checkSelfPermission(IndexActivity.this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(IndexActivity.this,new String[]{Manifest.permission.CAMERA},1); }
        if (ContextCompat.checkSelfPermission(IndexActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(IndexActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
        } else {
            startActivityForResult(new Intent(IndexActivity.this, CaptureActivity.class), 0);
        }
    }

    //今日
    public void today(View v) {
        Intent intent = new Intent(IndexActivity.this, TodayActivity.class);
        startActivity(intent);
    }

    //库存
    public void store(View v) {
        Intent intent = new Intent(IndexActivity.this, GoodsStoreActivity.class);
        startActivity(intent);
        //   Toast.makeText(this,"点击了库存",Toast.LENGTH_LONG).show();

    }


    /**
     * 处理上一个Activity的信息
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            Bundle bundle = data.getExtras();
            //   Toast.makeText(this,"扫描结果："+bundle.getString("result"),Toast.LENGTH_LONG).show();
            //查询数据库
            String scanCode = bundle.getString("result");
            GoodsEntity user = GoodsEntityService.getOneByNumber(this, scanCode);
            //如果在数据库中不存在，那么就跳转到添加中
            if (user == null) {
                Intent intent = new Intent(IndexActivity.this, GoodsAddActivity.class);
                intent.putExtra("scanCode", scanCode);
                startActivity(intent);
            } else {
                Intent intent = new Intent(IndexActivity.this, GoodsShowActivity.class);
                intent.putExtra("scanCode", scanCode);
                startActivity(intent);
                // Toast.makeText(this, "看看吧", Toast.LENGTH_LONG).show();
            }
        }
    }

/*

    @SuppressLint("NewApi")
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        //添加标题栏的确定按钮
      //  MenuItem item=menu.add(0,MENU_CONFIRM,0,R.string.operation_confirm);

        /**
         * setShowAsAction参数说明   MenuItem接口的一些常量
         * SHOW_AS_ACTION_ALWAYS   总是显示这个项目作为一个操作栏按钮。
         * SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW   此产品的动作视图折叠成一个正常的菜单项。
         * SHOW_AS_ACTION_IF_ROOM   显示此项目作为一个操作栏的按钮,如果系统有空间。
         * SHOW_AS_ACTION_NEVER     从不显示该项目作为一个操作栏按钮。
         * SHOW_AS_ACTION_WITH_TEXT  当这个项目是在操作栏中,始终以一个文本标签显示它,即使它也有指定一个图标。

        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);//主要是这句话

        item.setOnMenuItemClickListener(listener);//添加监听事件
        item.setIcon(R.drawable.ic_menu_confirm);//设置图标

        return true;
    }

    MenuItem.OnMenuItemClickListener listener=new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case 1:
                    Toast.makeText(IndexActivity.this, "您点击了确定按钮", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
            return false;
        }
    };
  */


}
